package com.yxlgateway.gateway.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yxlcommon.common.pojo.Orders;
import com.yxlcommon.common.pojo.RespBean;
import com.yxlcommon.common.pojo.User;
import com.yxlgateway.service.OrdersService;
import com.yxlcommon.common.vo.OrderItemVo;
import com.yxlcommon.common.vo.OrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author: yxl
 * @since: 2021-12-22
 **/
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    @PostMapping("/addOrder")
    public RespBean addOrder(@RequestBody OrderItemVo order) throws Exception {
        return RespBean.operationSuccess("提交订单成功！", 200, ordersService.addOrder(order));
    }

    @PostMapping("/getPersonalOrders")
    public RespBean getPersonalOrders(@RequestBody User user) {
        return RespBean.operationSuccess("查询个人订单成功！", 200, ordersService.selectPersonalOrders(user.getUsername()));
    }

    @PostMapping("/uploadFile")
    public RespBean uploadFile(MultipartFile file, Orders orders) throws Exception {
        return ordersService.uploadFile(file, orders);
    }

    @PostMapping("/downloadFile")
    public void downloadFile(@RequestBody File file, HttpServletResponse resp) {
        ordersService.downloadFile(file.getName(), resp);
    }

    @PostMapping("/addRemark")
    public RespBean addRemarks(@RequestBody Orders orders) {
        return ordersService.addRemarks(orders);
    }

    @PostMapping("/getOrderDetail")
    public RespBean getOrderDetail(@RequestBody Orders orders) {
        return ordersService.getOrderDetailByOrderNum(orders.getOrderNum());
    }

    @PostMapping("/getOrderByOrderId")
    public RespBean getOrderByOrderId(@RequestBody Orders orders){
        return ordersService.selectOrderDetailById(orders.getOrderId());
    }

    @PostMapping("/receiveOrder")
    public RespBean receiveOrder(@RequestBody OrdersVo orders) {
        return RespBean.operationSuccess("接受成功", 200, ordersService.updateReceiveOrder(orders));
    }

    @PostMapping("/rejectOrder")
    public RespBean rejectOrder(@RequestBody Orders orders) {
        return RespBean.operationSuccess("拒绝成功", 200, ordersService.updateRejectOrder(orders));
    }

    @PostMapping("/selectAllOrderByPage")
    public RespBean<IPage<OrdersVo>> selectAllOrderByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                                        @RequestParam(defaultValue = "10") Integer size,
                                                         @RequestBody Orders orders){
        return ordersService.selectAllOrderByPage(currentPage, size, orders);
    }

    @PostMapping("/adminUploadFile")
    public RespBean adminUploadFile(MultipartFile file) throws IOException {
        return ordersService.adminUploadFile(file);
    }

    @PostMapping("/adminAddOrder")
    public RespBean<Integer> adminAddOrder(@RequestBody Orders orders){
        return ordersService.adminAddOrder(orders);
    }

    @PostMapping("/changeOrderById")
    public RespBean<Integer> changeOrderById(@RequestBody Orders orders){
        return ordersService.changeOrderById(orders);
    }

    @PostMapping("/adminDeleteOrderById")
    public RespBean<Integer> adminDeleteOrderById(@RequestBody Orders orders){
        return ordersService.adminDeleteOrderById(orders);
    }

    @PostMapping("/selectAllOrders")
    public RespBean<List<OrdersVo>> selectAllOrders(){
        return ordersService.selectAllOrders();
    }

    @PostMapping("/selectOrdersByDay")
    public RespBean<Integer> selectOrdersByDay(@RequestBody OrdersVo ordersVo){
        return ordersService.getOrdersByDay(ordersVo.getDay());
    }
}
